﻿
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace PluginDB
{
    public class CommonRepository
    {
        private readonly Repository<Category, PluginDbEntities> _categoryRepository;
        private readonly Repository<Source, PluginDbEntities> _sourceRepository;
        private readonly Repository<Site, PluginDbEntities> _siteRepository;

        public CommonRepository(PluginDbEntities pluginDbEntities)
        {
            _categoryRepository = new Repository<Category, PluginDbEntities>(pluginDbEntities);
            _sourceRepository = new Repository<Source, PluginDbEntities>(pluginDbEntities);
            _siteRepository = new Repository<Site, PluginDbEntities>(pluginDbEntities);
        }

        #region Create Methods
        public Site CreateSite(string name)
        {
            var site = new Site
            {
                SiteName = name,
                SiteURL = name
            };

            _siteRepository.Add(site);
            return site;
        }

        public Source CreateSource(string sourceName)
        {
            var source = new Source { SourceName = sourceName };

            _sourceRepository.Add(source);
            return source;
        }

        public Category CreateCategory(string name)
        {
            var category = new Category
            {
                CategoryName = name
            };

            _categoryRepository.Add(category);
            return category;
        } 
        #endregion

        public Source GetSource(string name)
        {
            return _sourceRepository.DoQuery(
                new Specification<Source>(s => s.SourceName.ToLower().Equals(name.ToLower()))).FirstOrDefault() ??
                         CreateSource(name);
        }

        public Category GetCategory(string name)
        {
            return _categoryRepository.DoQuery(
                new Specification<Category>(a => a.CategoryName.ToLower().Equals(name.ToLower()))).FirstOrDefault() ??
                           CreateCategory(name);
        }

        public Site GetSite(string name)
        {
            return _siteRepository.DoQuery(
                new Specification<Site>(s => s.SiteName.Equals(name))).FirstOrDefault() ?? CreateSite(name);
        }

        public List<Category> GetCategoriesList()
        {
            var categories = _categoryRepository.SelectAll().ToList();
            return categories;
        }

        public List<Site> GetSitesNames()
        {
            return _siteRepository.SelectAll().ToList();
        }

        internal void InsertMegaVideoCookie(string cookieCode, System.DateTime expiredDate)
        {
            var category = _categoryRepository.Session.Categories
                .Where(c => c.CategoryName == cookieCode).FirstOrDefault();

            if (category != null && category.CategoryName != cookieCode)
            {
                category.CategoryName = cookieCode;
                _categoryRepository.AddOrAttach(category);
            }

            if (category == null)
            {
                category = new Category
                                   {
                                       CategoryName = cookieCode
                                   };
                _categoryRepository.Add(category);
            }

            if(category.EntityState == EntityState.Unchanged) return;

            _categoryRepository.Save();
        }
    }
}
